﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GBPVRX2.UiSupport;
using System.Collections;
using System.Drawing;
using GBPVRLibrary.UtilityLibrary;
using GBPVR.Public;

namespace GBPVRLibrary.ControlLibrary
{
    public class PopupBox:GuiElement
    {
        public string Title { get; set; }
        public string Message { get; set; }

        private static UiStatic UiPopupBox { get; set; }
        protected static Hashtable Parameters { get; set; }

        protected float? ButtonVerticalSpacing { get; set; }
        protected float? ButtonHorizontalSpacing { get; set; }
        protected PointF ButtonCenterLocation { get; set; }

        public enum PopupButtonType { None, OK, Cancel, OKCancel, Yes, No, YesNo };

        public PopupButtonType ButtonType { get; protected set; }

        private List<GBPVRUiElement> NormalButtons { get; set; }
        private List<GBPVRUiElement> SelectedButtons { get; set; }

        public int SelectedButtonIndex { get; set; }

        public PopupBox(PopupButtonType ButtonType)
        {
            Animation = AnimationType.Fade | AnimationType.Stretch;
            this.ButtonType = ButtonType;
            if (Parameters == null)
                Parameters = new Hashtable();
            if (UiPopupBox == null)
                UiPopupBox = new UiStatic(SkinHelper, "PopupBox", Parameters);
            if (ButtonVerticalSpacing == null)
            {
                ButtonCenterLocation = SkinHelper.getPlacementAttributePos("PopupBox", "buttonCenterLocation");
                ButtonHorizontalSpacing = SkinHelper.getPlacementAttributeElementFloatValue("PopupBox", "buttonHorizontalSpacing");
                ButtonVerticalSpacing = SkinHelper.getPlacementAttributeElementFloatValue("PopupBox", "buttonVerticalSpacing");
            }
            NormalButtons = new List<GBPVRUiElement>();
            SelectedButtons = new List<GBPVRUiElement>();
            switch (ButtonType)
            {
                case PopupButtonType.OKCancel:
                case PopupButtonType.YesNo:

                    break;
                case PopupButtonType.Cancel:
                    {
                        Hashtable parameters = new Hashtable();
                        parameters["@Text"] = ButtonType.ToString();
                        RectangleF rectNormal = SkinHelper.getNamedImageRect("PopupBox-Button-Normal");
                        RectangleF rectSelected = SkinHelper.getNamedImageRect("PopupBox-Button-Selected");
                        Image imgNormal = SkinHelper.getNamedImage("PopupBox-Button-Normal", parameters);
                        Image imgSelected = SkinHelper.getNamedImage("PopupBox-Button-Selected", parameters);
                        RectangleF locNormal = new RectangleF(ButtonCenterLocation.X - (ButtonHorizontalSpacing.Value / 2), ButtonCenterLocation.Y - (ButtonVerticalSpacing.Value / 2), rectNormal.Width, rectNormal.Height);
                        RectangleF locSelected = new RectangleF(ButtonCenterLocation.X - (ButtonHorizontalSpacing.Value / 2), ButtonCenterLocation.Y - (ButtonVerticalSpacing.Value / 2), rectSelected.Width, rectSelected.Height);
                        NormalButtons.Add(new GBPVRUiElement("PopupBox-Button-Normal-" + ButtonType.ToString(), locNormal, imgNormal));
                        SelectedButtons.Add(new GBPVRUiElement("PopupBox-Button-Selected-" + ButtonType.ToString(), locSelected, imgSelected));
                    }
                    break;
            }
        }

        protected override void Render(System.Collections.ArrayList RenderList, ref bool RequiresMoreRendering)
        {
            Parameters["@Title"] = Title;
            Parameters["@Message"] = Message;
            UiPopupBox.SetArgs(Parameters);
            RenderList.AddRange(UiPopupBox.GetRenderList());
            for (int i = 0; i < NormalButtons.Count; i++)
            {
                if (i == SelectedButtonIndex)
                    RenderList.Add(SelectedButtons[i]);
                else
                    RenderList.Add(NormalButtons[i]);
            }        
        }
        /*
        protected override void PostRender(ArrayList RenderList)
        {
            if (TimeShown > DateTime.Now.AddSeconds(-0.5))
            {
                if (AlphaElements.Count == 0)
                {
                    foreach (GBPVRUiElement el in RenderList)
                    {
                        AlphaElements[el] = el.alpha;
                        ElementLocations[el] = el.rect;
                    }
                }
                float diff = (float)(DateTime.Now - TimeShown).TotalMilliseconds;
                diff /= 500; // 0.5 seconds;
                float stretch = 0.15f;
                // animate
                foreach (GBPVRUiElement el in RenderList)
                {
                    el.alpha = (int)(AlphaElements[el] * diff);
                    RectangleF rect = new RectangleF();
                    //rect.X = ElementLocations[el].X + (ElementLocations[el].Width / 2) + (ElementLocations[el].Width / 2) * diff;
                    //rect.Y = ElementLocations[el].Y + (ElementLocations[el].Height / 2) + (ElementLocations[el].Height / 2) * diff;
                    //rect.Width = ElementLocations[el].Width - (ElementLocations[el].Width / 2) + (ElementLocations[el].Width / 2) * diff;
                    //rect.Height = ElementLocations[el].Height - (ElementLocations[el].Height / 2) + (ElementLocations[el].Height / 2) * diff;
                    //el.SetRect(rect);
                    el.textureTopLeft.X = -stretch + (stretch * diff);
                    el.textureBottomLeft.X = -stretch + (stretch * diff);
                    el.textureTopRight.X = (1f + stretch) - (stretch * diff);
                    el.textureBottomRight.X = (1f + stretch) - (stretch * diff);

                    el.textureTopLeft.Y = -stretch + (stretch * diff);
                    el.textureTopRight.Y = -stretch + (stretch * diff);
                    el.textureBottomLeft.Y = (1f + stretch) - (stretch * diff);
                    el.textureBottomRight.Y = (1f + stretch) - (stretch * diff);
                }
                GuiElement.NeedsRendering = true;
            }
            else if (AlphaElements.Count > 0)
            {
                foreach (GBPVRUiElement el in RenderList)
                {
                    el.alpha = AlphaElements[el];
                    //el.SetRect(ElementLocations[el]);
                    
                    el.textureTopLeft.X = 0;
                    el.textureBottomLeft.X = 0;
                    el.textureTopRight.X = 1;
                    el.textureBottomRight.X = 1;
                    el.textureTopLeft.Y = 0;
                    el.textureTopRight.Y = 0;
                    el.textureBottomLeft.Y = 1;
                    el.textureBottomRight.Y = 1;
                }
                AlphaElements.Clear(); // since we are done
                ElementLocations.Clear();
                GuiElement.NeedsRendering = true;
            }
        }
        */
        public override bool OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            switch (KeyMapper.GetControlKey(e))
            {
                case KeyMapper.ControlKey.Back:
                    if (Cancel())
                        this.Visible = false;
                    return true;
                case KeyMapper.ControlKey.Enter:
                    switch (this.ButtonType)
                    {
                        case PopupButtonType.Cancel:
                        case PopupButtonType.No:
                            if (Cancel())
                                this.Visible = false;
                            break;
                        case PopupButtonType.OK:
                        case PopupButtonType.Yes:
                            break;
                        case PopupButtonType.YesNo:
                        case PopupButtonType.OKCancel:
                            if (SelectedButtonIndex == 1) // no|cancel
                            {
                                if (Cancel())
                                    this.Visible = false;
                            }
                            else
                            {
                            }
                            break;
                    }
                    return true;
                case KeyMapper.ControlKey.DirectionUp:
                case KeyMapper.ControlKey.DirectionLeft:
                    if (this.ButtonType == PopupButtonType.None)
                        break;
                    if (SelectedButtonIndex - 1 < 0)
                        SelectedButtonIndex = this.NormalButtons.Count - 1;
                    else
                        SelectedButtonIndex--;
                    break;
                case KeyMapper.ControlKey.DirectionDown:
                case KeyMapper.ControlKey.DirectionRight:
                    if (this.ButtonType == PopupButtonType.None)
                        break;
                    if (SelectedButtonIndex + 1 >= this.NormalButtons.Count)
                        SelectedButtonIndex = 0;
                    else
                        SelectedButtonIndex++;
                    break;
            }
            return base.OnKeyDown(e);
        }

        protected virtual bool Cancel()
        {
            return false;
        }

    }
}
